package com.umeox.watch.prayertime.bean;

import android.os.Parcel;
import android.os.Parcelable;

import com.umeox.moto.common.utils.DateUtils;
import com.umeox.watch.moto.dataservice.DataProvider;
import com.umeox.watch.prayertime.utils.Utils;

import java.util.Calendar;

/**
 * Created by Rambo 2021/11/16
 * Describe:
 */
public class PrayerAlarmDetail implements Parcelable, Comparable<PrayerAlarmDetail>, Cloneable {

    public int id;
    public String prayerName;
    public int reminder;  //提前多少时间，单位分钟
    public String reminderTime; //提醒时间
    public String prayerTime; //祈祷时间

    /**
     * 闹钟具体时间戳
     */
    public long alarmTimeInMillis;


    public PrayerAlarmDetail() {

    }

    protected PrayerAlarmDetail(Parcel in) {
        id = in.readInt();
        prayerName = in.readString();
        reminder = in.readInt();
        reminderTime = in.readString();
        prayerTime = in.readString();
    }


    public static PrayerAlarmDetail convertFromValues(PrayerTimeInfo prayerTimeInfo) {
        if (prayerTimeInfo == null) {
            return null;
        }
        PrayerAlarmDetail detail = new PrayerAlarmDetail();
        detail.id = prayerTimeInfo.getIndex();
        detail.prayerName = prayerTimeInfo.getPrayerTimeName();
        detail.reminder = DataProvider.getPrayerReminder();
        detail.prayerTime = prayerTimeInfo.getPrayerTime();
        if(prayerTimeInfo.getIndex() == 1){ //Sunrise 时间
            detail.reminderTime = prayerTimeInfo.getPrayerTime();
        }else{
            detail.reminderTime =  DateUtils.formatTime(prayerTimeInfo.getTimestamp() - ((long) DataProvider.getPrayerReminder() * 60 * 1000));
        }

        return detail;
    }


    @Override
    public String toString() {
        return "PrayerAlarmDetail{" +
                "id=" + id +
                ", prayerName='" + prayerName + '\'' +
                ", reminder=" + reminder +
                ", reminderTime='" + reminderTime + '\'' +
                ", prayerTime='" + prayerTime + '\'' +
                ", alarmTimeInMillis=" + alarmTimeInMillis +
                '}';
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.id);
        dest.writeString(this.prayerName);
        dest.writeInt(this.reminder);
        dest.writeString(this.reminderTime);
        dest.writeString(this.prayerTime);
    }


    public static final Creator<PrayerAlarmDetail> CREATOR = new Creator<PrayerAlarmDetail>() {
        @Override
        public PrayerAlarmDetail createFromParcel(Parcel in) {
            return new PrayerAlarmDetail(in);
        }

        @Override
        public PrayerAlarmDetail[] newArray(int size) {
            return new PrayerAlarmDetail[size];
        }
    };

    @Override
    public int compareTo(PrayerAlarmDetail another) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, getHour());
        cal.set(Calendar.MINUTE, getMinute());
        long temp1 = cal.getTimeInMillis();
        cal.set(Calendar.HOUR_OF_DAY, another.getHour());
        cal.set(Calendar.MINUTE, another.getMinute());
        long temp2 = cal.getTimeInMillis();
        return Long.compare(temp1, temp2);
    }

    public int getHour() {
        return Integer.parseInt(getHourString());
    }

    public String getHourString() {
        if (Utils.is24HourFormatTime(reminderTime)) {
            return reminderTime.split(":")[0];
        } else {
            return DateUtils.formatDateTime(System.currentTimeMillis(), "HH");
        }
    }

    public int getMinute() {
        return Integer.parseInt(getMinuteString());
    }


    public String getMinuteString() {
        if (Utils.is24HourFormatTime(reminderTime)) {
            return reminderTime.split(":")[1];
        } else {
            return DateUtils.formatDateTime(System.currentTimeMillis(), "mm");
        }
    }
}
